package com.onegroup.config;

import com.onegroup.filter.JwtAuthenticationTokenFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * @author: 曾雪斌
 * @Description:
 * @History:
 */
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private AuthenticationConfiguration authenticationConfiguration;
    @Autowired
    private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;
    @Autowired
    private AuthenticationEntryPoint authenticationEntryPoint;
    @Autowired
    private AccessDeniedHandler accessDeniedHandler;


    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
//        super.configure(http);
        http
                // 关闭跨站请求伪造
                .csrf().disable()
                // 不通过session获取SecurityContext
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                // 只允许匿名(未登录)访问
                .antMatchers("/auth/application/loginByPwd").anonymous()
                // 认证(登录)了才能访问
                .antMatchers("/test").authenticated()
                // 权限
                .antMatchers("/auth").hasAuthority("auth")
                // 角色
                .antMatchers("/role").hasRole("role")
                // 无论登不登录都能访问
                .antMatchers("/dataAnalysis/*").permitAll()
                .antMatchers("/ProjectAcceptanceService").permitAll()
                // 除了上面外的所有请求全部需要鉴权认证
                .anyRequest().authenticated();
        System.out.println("2213213");

        // 把jwt过滤器放在UsernamePasswordAuthenticationFilter之前
        http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);

        // 处理异常
        http.exceptionHandling().authenticationEntryPoint(authenticationEntryPoint)
                .accessDeniedHandler(accessDeniedHandler);

        //允许跨域
        http.cors();
    }

//    @Bean
    public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
        httpSecurity
                // 关闭跨站请求伪造
                .csrf().disable()
                // 不通过session获取SecurityContext
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                // 只允许匿名(未登录)访问
                .antMatchers("/auth/application/loginByPwd").anonymous()
                // 认证(登录)了才能访问
                .antMatchers("/test").authenticated()
                // 权限
                .antMatchers("/auth").hasAuthority("auth")
                // 角色
                .antMatchers("/role").hasRole("role")
                // 无论登不登录都能访问
                .antMatchers("/dataAnalysis/*").permitAll()
                .antMatchers("/ProjectAcceptanceService/*").permitAll()
                // 除了上面外的所有请求全部需要鉴权认证
                .anyRequest().authenticated();

        // 把jwt过滤器放在UsernamePasswordAuthenticationFilter之前
        httpSecurity.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);

        // 处理异常
        httpSecurity.exceptionHandling().authenticationEntryPoint(authenticationEntryPoint)
                .accessDeniedHandler(accessDeniedHandler);

        //允许跨域
        httpSecurity.cors();
        return httpSecurity.build();
    }

    @Bean
    public AuthenticationManager authenticationManager() throws Exception {
        return authenticationConfiguration.getAuthenticationManager();
    }

}
